package com.gqz.equip.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import com.gqz.common.core.domain.RequestForm;
import com.gqz.common.core.utils.query.QueryMapperUtil;
import com.gqz.equip.entity.TEnWzSblxEntity;
import com.gqz.equip.mapper.TEnWzSblxMapper;
import com.gqz.equip.mapstruct.TEnWzSblxMapstruct;
import com.gqz.equip.service.ITEnWzSblxService;
import com.gqz.equip.vo.TEnWzSblxVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;


/**
 * 新能源-物资-设备类型树 业务逻辑实现层ServiceImpl
 *
 * @Version 1.0
 * @Author input you name
 */
@Service
@Slf4j
@CacheConfig(cacheNames = "sblx")
public class TEnWzSblxServiceImpl extends ServiceImpl<TEnWzSblxMapper, TEnWzSblxEntity> implements ITEnWzSblxService {

    @Autowired
    private TEnWzSblxMapstruct mapstruct;

    @Override
    @Cacheable(key = "#id", unless = "#result == null || #result.size() == 0")
    public List<TEnWzSblxVo> getById(String id) {
        MPJLambdaWrapper<TEnWzSblxEntity> queryWrapper = new MPJLambdaWrapper<>();
        /** todo getPsrId-> todo-must修改为记录表主键字段todo */
        queryWrapper.eq(TEnWzSblxEntity::getId, id);
        queryWrapper.selectAll(TEnWzSblxEntity.class);
        List<TEnWzSblxEntity> entities = baseMapper.selectList(queryWrapper);
        if (CollectionUtils.isNotEmpty(entities)) {
            return mapstruct.toVos(entities);
        }
        return new ArrayList<>();
    }

    @Override
    @Cacheable(key = "#requestForm.hashCode()")
    public Page<TEnWzSblxVo> page(RequestForm<TEnWzSblxVo> requestForm) {
        TEnWzSblxEntity entity = mapstruct.toEntity(requestForm.getEntity());
        MPJLambdaWrapper<TEnWzSblxEntity> queryWrapper = QueryMapperUtil.initQueryWrapper(entity, requestForm);
        queryWrapper.selectAll(TEnWzSblxEntity.class);
        Page<TEnWzSblxEntity> page = Page.of(requestForm.getPageNo(), requestForm.getPageSize());
        //page = baseMapper.selectPage(page, queryWrapper);
        IPage<TEnWzSblxEntity> iPage = baseMapper.selectJoinPage(page, TEnWzSblxEntity.class, queryWrapper);
        page.setRecords(iPage.getRecords());
        page.setTotal(iPage.getTotal());
        return mapstruct.toVos(page);
    }

    @Override
    public Long count(RequestForm<TEnWzSblxVo> requestForm) {
        TEnWzSblxEntity entity = mapstruct.toEntity(requestForm.getEntity());
        MPJLambdaWrapper<TEnWzSblxEntity> queryWrapper = QueryMapperUtil.initQueryWrapper(entity, requestForm);
        /** todo getPsrId-> todo-must修改为记录表主键字段todo */
        queryWrapper.select(TEnWzSblxEntity::getId);
        return baseMapper.selectCount(queryWrapper);
    }

    @Override
    @CachePut(key = "#vos.hashCode()")
    public int insert(List<TEnWzSblxVo> vos) {
        List<TEnWzSblxEntity> entitys = mapstruct.toEntitys(vos);
        int result = 0;
        for (int i = 0, size = entitys.size(); i < size; i++) {
            TEnWzSblxEntity entity = entitys.get(i);
            result += baseMapper.insert(entity);
        }
        return result;
    }

    @Override
    @CacheEvict(key = "#vos.hashCode()")
    public int update(List<TEnWzSblxVo> vos) {
        List<TEnWzSblxEntity> entitys = mapstruct.toEntitys(vos);
        int result = 0;
        for (int i = 0, size = entitys.size(); i < size; i++) {
            TEnWzSblxEntity entity = entitys.get(i);
            result += baseMapper.updateById(entity);
        }
        return result;
    }

    @Override
    @Cacheable(key = "#pid", unless = "#result == null || #result.size() == 0")
    public List<TEnWzSblxVo> queryLeaf(String pid) {
        MPJLambdaWrapper<TEnWzSblxEntity> queryWrapper = new MPJLambdaWrapper<>();

        queryWrapper.selectAll(TEnWzSblxEntity.class);
        queryWrapper.eq(TEnWzSblxEntity::getPid, pid);
        queryWrapper.eq(TEnWzSblxEntity::getSfsc, 0);
        //queryWrapper.eq(TEnInterfaceCodeEntity::getValue, value);
        queryWrapper.orderByAsc(TEnWzSblxEntity::getOrderNo);

        List<TEnWzSblxEntity> codeEntityList = baseMapper.selectList(queryWrapper);

        return mapstruct.toVos(codeEntityList);
    }

    @Override
    @Cacheable(key = "#iEnWzSblxVo.hashCode()")
    public List<TEnWzSblxVo> getList(TEnWzSblxVo iEnWzSblxVo) {
        MPJLambdaWrapper<TEnWzSblxEntity> queryWrapper = new MPJLambdaWrapper<>();

        queryWrapper.selectAll(TEnWzSblxEntity.class);
        if (iEnWzSblxVo.getIsLeaf() != null && !"".equals(iEnWzSblxVo.getIsLeaf()) && iEnWzSblxVo.getIsLeaf() != "") {
            queryWrapper.eq(TEnWzSblxEntity::getIsLeaf, iEnWzSblxVo.getIsLeaf());
        }
        if (iEnWzSblxVo.getPid() != null && !"".equals(iEnWzSblxVo.getPid()) && iEnWzSblxVo.getPid() != "") {
            queryWrapper.eq(TEnWzSblxEntity::getPid, iEnWzSblxVo.getPid());
        }
        queryWrapper.eq(TEnWzSblxEntity::getSfsc, 0);
        queryWrapper.orderByAsc(TEnWzSblxEntity::getOrderNo);
        List<TEnWzSblxEntity> codeEntityList = baseMapper.selectList(queryWrapper);

        return mapstruct.toVos(codeEntityList);
    }


}
